<script setup lang="ts">
import { fileImg } from '@/apis/consult';
import type { Image } from '@/types/consult';
import { showLoadingToast, showToast, type UploaderAfterRead } from 'vant';
import { ref } from 'vue';

defineProps<{
  disabled: boolean
}>()
const msg = ref('')
const emit = defineEmits<{
  'send-msg': [msg: string]
  'send-img': [img: Image]
}>()
//发送文本消息
const onSend = () => {
  //输入框没内容
  if (msg.value.trim() === '') return showToast('请输入内容')
  emit('send-msg', msg.value)
  msg.value = ''
}

//上传图片
const afterRead: UploaderAfterRead = async (item) => {
  if (Array.isArray(item)) return
  //没上传
  if (!item.file) return
  //上传了图片
  const st = showLoadingToast({
    message: '上传中...',
    duration: 0
  })
  const res = await fileImg(item.file)
  st.close()
  emit('send-img', res.data)
}
</script>

<template>
  <div class="room-action">
    <van-field type="text" class="input" :border="false" placeholder="问医生" autocomplete="off" v-model="msg"
      @keydown.enter="onSend" :disabled="disabled"></van-field>
    <van-uploader :preview-image="false" :disabled="disabled" :after-read="afterRead">
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;

  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }

  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>
